<div th:replace="admin/module/_header"></div>
<div class="wrapper">
    <!-- 菜单栏模块 -->
    <div th:replace="admin/module/_sidebar"></div>
    <div class="content-wrapper">
        <section class="content-header">
            <h1>
                标签列表
                <small></small>
            </h1>
            <ol class="breadcrumb">
                <li>
                    <a data-pjax="true" href="/admin">
                        <i class="fa fa-dashboard"></i> 首页</a>
                </li>
                <li><a data-pjax="true" href="#">标签管理</a></li>
                <li class="active">标签列表</li>
            </ol>
        </section>
        <section class="content container-fluid">
            <div class="row">
                <div class="col-md-5" th:if="${updateTag == null}">
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h3 class="box-title">添加标签</h3>
                        </div>
                        <form action="/admin/tag/save" method="post" role="form" id="tagForm">
                            <div class="box-body">
                                <div class="form-group">
                                    <label for="tagName">标签名称</label>
                                    <input type="text" class="form-control" id="tagName" name="tagName"
                                           placeholder="">
                                </div>
                            </div>
                            <div class="box-footer">
                                <button type="button" class="btn btn-primary btn-sm " onclick="saveTag()">保存
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="col-md-5" th:if="${updateTag != null}">
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h3 class="box-title">更新标签</h3>
                        </div>
                        <form action="/admin/tag/save" method="post" role="form" id="tagForm">
                            <input type="hidden" name="id" th:value="${updateTag.id}">
                            <div class="box-body">
                                <div class="form-group">
                                    <label for="tagName">标签名称</label>
                                    <input type="text" class="form-control" id="tagName" name="tagName"
                                           placeholder="" th:value="${updateTag.tagName}">
                                </div>
                            </div>
                            <div class="box-footer">
                                <button type="button" class="btn btn-primary btn-sm " onclick="saveTag()">保存
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="col-md-7">
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h3 class="box-title">所有标签</h3>
                        </div>
                        <div class="box-body table-responsive">
                            <table class="table table-hover">
                                <thead>
                                <tr>
                                    <th>标签名称</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr th:each="tag : ${tags}">
                                    <td>
                                        <a th:href="'/tag/'+${tag.id}" target="_blank">[[${tag.tagName}]]</a>
                                    </td>
                                    <td>
                                        <a data-pjax="true"
                                           th:href="'/admin/tag/edit?id='+${tag.id}+'&page='+${pageInfo.current}"
                                           class="btn btn-primary btn-xs ">编辑</a>
                                        <button class="btn btn-danger btn-xs delete-tag"
                                                th:onclick="'removeIt('+${tag.id}+')'">删除
                                        </button>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <div class="box-footer clearfix">
                            <div class="no-margin pull-left">
                                第 [[${pageInfo.current}]] / [[${pageInfo.pages}]] 页
                            </div>
                            <ul class="pagination no-margin pull-right">
                                <li><a data-pjax="true"  th:classappend="${pageInfo.current == 1 ? 'disabled' : ''}" class="btn btn-sm"
                                       href="/admin/tag">
                                    首页
                                </a>
                                </li>
                                <li><a data-pjax="true" class="btn btn-sm"  th:classappend="${pageInfo.current == 1 ? 'disabled' : ''}"
                                       th:href="'/admin/tag?page='+${pageInfo.current-1}">
                                    上一页
                                </a>
                                </li>
                                <li><a data-pjax="true" class="btn btn-sm"  th:classappend="${pageInfo.current == pageInfo.pages ? 'disabled' : ''}"
                                       th:href="'/admin/tag?page='+${pageInfo.current+1}">
                                    下一页
                                </a>
                                </li>
                                <li><a data-pjax="true" class="btn btn-sm" th:classappend="${pageInfo.current == pageInfo.pages ? 'disabled' : ''}"
                                       th:href="'/admin/tag?page='+${pageInfo.pages}">
                                    尾页
                                </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <script>

            function saveTag() {
                $.ajax({
                    type: 'POST',
                    url: '/admin/tag/save',
                    async: false,
                    data: $("#tagForm").serialize(),
                    success: function (data) {
                        if (data.code == 0) {
                            showMsg(data.msg, "error", 1000);
                        } else {
                            showMsgAndRedirect(data.msg, "success", 1000, "/admin/tag");
                        }
                    }
                });
            };

            function removeIt(id) {
                layer.confirm('请确认是否删除', function (index) {
                    $.ajax({
                        type: 'DELETE',
                        url: '/admin/tag/delete',
                        async: false,
                        data: {
                            'id': id
                        },
                        success: function (data) {
                            if (data.code == 0) {
                                showMsg(data.msg, "error", 1000);
                            } else {
                                showMsgAndRedirect(data.msg, "success", 1000, "/admin/tag");
                            }

                        }
                    });
                    layer.close(index);
                });
            }

        </script>
    </div>

</div>
<div th:replace="admin/module/_footer"></div>
